/**
 * hnjz.com Inc.
 * Copyright (c) 2004-2013 All Rights Reserved.
 */
package com.hnjz.sys.function;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hnjz.common.AppException;
import com.hnjz.common.JsonResultUtil;
import com.hnjz.common.util.LogUtil;

/**
 * 功能管理的Controller
 * 
 * @author lijingfen
 * @version $Id: FunController.java,$
 */
@Controller
public class FunController {

    /**日志*/
    private static final Log log = LogFactory.getLog(FunController.class);

    @Autowired
    private FunManager      funManager;

    /**
     * 到功能管理的初始界面
     * 
     * @param model {@link ModelMap}
     * @return 功能管理的初始界面
     */
    @RequestMapping(value = "/function.htm")
    public String function(ModelMap model) {
        return "sys/function/function";
    }

    /**
     * 用户管理的查询
     * 
     * @param model {@link ModelMap}
     * @param name 搜索条件，可以按名称，备注搜索
     * @param page 第几页
     * @param pageSize 每页显示多少条
     */
    @RequestMapping(value = "/funQuery.json", produces = "application/json")
    public void funQuery(ModelMap model, String name) {
     
        try {
            JSONArray re = funManager.queryFun(name);
            model.addAttribute("re", re.toString());
        } catch (Exception e) {
            log.error("查询出错：", e);
        }
    }

    /**
     * 删除一个功能
     * 
     * @param model  {@link ModelMap}
     * @param id 功能id
     */
    @RequestMapping(value = "/delFun.json", produces = "application/json")
    public void delFun(ModelMap model, String id) {
        try {
            funManager.removeFun(id);
            JsonResultUtil.suncess(model, "删除成功！");
        } catch (AppException e) {
            log.error("删除功能信息错误：", e);
            JsonResultUtil.fail(model, e.getMessage());
        } 
        catch (Exception e) {
            log.error("删除功能信息错误：", e);
            JsonResultUtil.fail(model, "删除失败！");
        }
    }

    /**
     * 保存一个功能信息
     * 
     * @param model {@link ModelMap}
     * @param funForm  功能管理的form表单
     * @return 功能管理的初始界面
     */
    @RequestMapping(value = "/saveFun.json", produces = "application/json")
    public void saveFun(ModelMap model, FunForm frm) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("表单信息:" + LogUtil.m(frm));
            }
            funManager.saveFun(frm);
            JsonResultUtil.suncess(model, "保存成功！");
        } catch (AppException e) {
            log.error("保存信息错误：", e);
            JsonResultUtil.fail(model, e.getMessage());
        } catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    /**
     * 编辑一个功能信息
     * 
     * @param id 功能Id
     * @return 功能管理的初始界面
     */
    @RequestMapping(value = "/editFun.htm")
    public String editFun(ModelMap model, FunForm frm) {
        if (log.isDebugEnabled()) {
            log.debug("id:" + frm.getId());
        }
        if (StringUtils.isBlank(frm.getId())) {
            return "sys/function/functionEdit";
        }
        Function po = this.funManager.getFun(frm.getId());
        frm.setId(po.getId());
        frm.setFuncDesc(po.getFuncDesc());
        frm.setFuncName(po.getFuncName());
        frm.setOrderby(String.valueOf(po.getOrderby()));
        if (null != po.getParent()) {
        	Function f = this.funManager.getFun(po.getParent());
        	if(null != f){
                frm.setParent(f.getId());
                frm.setParentName(f.getFuncName());
        	}
        }
        frm.setLinkaddr(po.getLinkaddr());
       
        return "sys/function/functionEdit";
    }
    /**
     * 菜单的公共选择界面
     * 
     * @param id 菜单Id
     * @return 菜单功能的初始界面
     */
    @RequestMapping(value = "/funPubQuery.htm")
    public String funPubQuery(ModelMap model, String id) {
        try {
            JSONArray re = this.funManager.querySelectFunction();
            model.addAttribute("menu", re.toString());
        } catch (Exception e) {
            log.error("查询菜单信息错误：", e);
        }
        return "sys/function/functionPubQuery";
    }
    /**
     * 查询当前菜单有那些操作
     * 
     * @param id 菜单Id
     * @return 菜单有那些操作
     */
    @RequestMapping(value = "/functionOpt.json")
    @ResponseBody
    public List<FuncOper> functionOpt(ModelMap model, String id) {
        try {
            return this.funManager.queryOptByFunction(id);
        } catch (Exception e) {
            log.error("查询错误：", e);
        }
        return null;
    }
}
